package org.dtrd.modules.record.controller;

import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.dtrd.base.BaseController;
import org.dtrd.common.api.vo.Result;
import org.dtrd.config.system.SystemState;
import org.dtrd.modules.base.service.BaseCommonService;
import org.dtrd.modules.record.entity.bean.SleepBean;
import org.dtrd.modules.record.entity.bean.SleepDailyRecord;
import org.dtrd.modules.record.entity.bean.SleepInfo;
import org.dtrd.modules.record.entity.request.TimePeriodRequest;
import org.dtrd.modules.record.service.ISleepRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import static org.dtrd.common.constant.CommonConstant.OPERATE_TYPE_QUERY;
import static org.dtrd.common.constant.CommonConstant.OPERATE_TYPE_SAVE_OR_UPDATE;

/**
 * @Author QR
 * @Date 2021/11/3 5:06 PM
 */
@Api(tags = "睡眠打卡")
@RestController
@RequestMapping("/101/10")
public class SleepRecordController extends BaseController {

    @Autowired
    private ISleepRecordService sleepRecordService;
    @Autowired
    private BaseCommonService baseCommonService;

    @ApiOperation(value = "分页查询睡眠记录列表", response = SleepDailyRecord.class)
    @PostMapping("/1011002")
    public Result<?> getSleepList(@RequestBody TimePeriodRequest request){
        Integer patientId = request.getPatientId();
        if (isIntParamInvalid(patientId)){
            return error(SystemState.ResponseState.STATE_PARAMS_MISSING);
        }
        String startDate = null;
        String endDate = null;
        String[] timePeriod = handleTimePeriod(request);
        if (timePeriod != null) {
            startDate = timePeriod[0];
            endDate = timePeriod[1];
        }
        if (startDate == null || endDate == null) {
            return error(SystemState.ResponseState.STATE_PARAMS_MISSING);
        }
        Integer pageNum = getIntParam(request.getPage(), 1);
        Integer pageSize = getPageSize(request.getPageSize(), 10);
        SleepInfo sleepInfo = sleepRecordService.getSleepRecordListByDateRange(patientId, pageNum, pageSize, startDate, endDate);
        String logContent = String.format("分页查询睡眠记录列表，查询患者id：%d", request.getPatientId());
        baseCommonService.addLog(logContent, OPERATE_TYPE_QUERY);
        return Result.OK(sleepInfo);
    }

    @ApiOperation("更新或修改睡眠记录")
    @PostMapping("/1011001")
    public Result<?> saveOrUpdateSleepRecord(@RequestBody SleepBean sleepBean){
        // 校验参数
        if (isIntParamInvalid(sleepBean.getPatientId())
                || StrUtil.isBlank(sleepBean.getRecordTime())){
            return error(SystemState.ResponseState.STATE_PARAMS_MISSING);
        }
        boolean result = sleepRecordService.saveOrUpdateSleepRecord(sleepBean);
        String logContent = String.format("更新或修改睡眠记录，患者id：%d", sleepBean.getPatientId());
        baseCommonService.addLog(logContent, OPERATE_TYPE_SAVE_OR_UPDATE);
        return result ? Result.OK() : error(SystemState.ResponseState.STATE_OPERATION_FAILURE);
    }
}
